package com.hn.zhijian.system.rsasdk;

import java.util.Optional;


/**
 * 解析实现
 */
public abstract class Resolve implements IResolve {
    /**
     * 得到ticket
     *
     * @param value
     * @return
     */
    @Override
    public String ticket(String value) {
        verPublic();
        return RSAUtils.decryptByPub(value.getBytes(), publicKey());
    }

    @Override
    public boolean verifySignature(String content, String sign) {
        verPublic();
        return RSASignUtils.getInstance().doCheck(ticket(content), sign, publicKey(), "UTF-8");
    }


    /**
     * 验证公钥
     */
    public void verPublic() {
        Optional.ofNullable(publicKey()).orElseThrow(() ->
                new ResolverException(ResolveException.KEY_NULL_EXCEPTION.getKey(), "公钥为空"));
    }

    /**
     * 验证私钥
     */
    public void verPrivate() {
        Optional.ofNullable(privateKey()).orElseThrow(()
                -> new ResolverException(ResolveException.KEY_NULL_EXCEPTION.getKey(), "私钥为空"));
    }
}
